// const my = ['root', 'dashboard', 'coins']

// const perms = [
//   {
//     router: 'root',
//     children: [
//       {
//         router: 'dashboard'
//       },
//       {
//         router: 'orders',
//         children: ['buy', 'sell']
//       },
//       {
//         router: 'balance',
//         children: ['details', 'report']
//       },
//       {
//         router: 'manual',
//         children: ['out', 'sdk']
//       },
//       { router: 'coins' },
//       {
//         router: 'config',
//         children: ['password', 'security', 'whitelist', 'sub']
//       }
//     ]
//   }
// ]

const map = new Map([
  ['profile', 'profile'],
  ['buy', 'orders'],
  ['sell', 'orders'],
  ['details', 'balance'],
  ['report', 'balance'],
  ['coins', 'coins'],
  ['out', 'manual'],
  ['sdk', 'manual'],
  ['password', 'config'],
  ['security', 'config'],
  ['sub', 'config'],
  ['whitelist', 'config'],
  ['ga', 'config'],
])

function getRoute (perms) {
  const arr = []
  if (perms === null) perms = Array.from(map.keys())
  if (perms.includes('password')) perms.push('password')
  // console.log(perms);
  perms.forEach((item) => {
    const val = map.get(item)
    if (!val) return

    const find = arr.find((item2) => item2.router === val)

    if (item === val && !find) {
      // ['coins', 'coins']
      arr.push({ router: val })
      return
    }

    if (find) {
      if (find.children && !find.children.includes(item)) {
        find.children.push(item)
      }
    } else {
      arr.push({
        router: val,
        children: [item],
      })
    }
  })
  console.log(arr);
  return [
    {
      router: 'root',
      children: [
        {
          router: 'dashboard'
        },
        ...arr
      ]
    }
  ]
}


export default getRoute
